<?php
namespace app\admin\controller;

use app\think\Controller;
use app\admin\model\Paper as PaperModel ;

/**
 * 
 * 考试模块的类
 *
 * @name Examination
 * @author Aninet Sat Jun  9 19:58:46 2018
 */
class Examination extends Base{

    /**
     * 试卷的列表页面 
     * 
     * @return 
     * @date Sat Jun  9 19:57:13 2018
     */
    public function paper(){
        $model = new PaperModel;
        $data = $model->field(['pap_id','pap_name','pap_type','pap_mark','pap_createtime'])->select();
        if($data){
            $this->assign('data',$data);
            return $this->fetch();
            
        }else{
            return $this->error("无数据");
        }
    }

    /**
     * 创建试卷的页面；
     * 
     * @return html
     * @date Sun Jun 10 18:28:53 2018
     */
    public function createpaper(){
        return $this->fetch();
    }

    /**
     * 插入试卷数据到数据库
     * 
     * @return success/error
     * @date Sun Jun 10 18:59:51 2018
     */
    public function insertpaper(){
        //接受三个数据；
        $_data = $this->req->post();
        $data['pap_name'] = $_data['name'];
        $data['pap_type'] = $_data['type'];
        $data['pap_mark'] = $_data['mark'];
        $data['pap_createtime'] = time();
        $paperModel = new \app\admin\model\Paper;
        $result = $paperModel->save($data);
        if($result){
            return $this->success('创建试卷成功');
        }else{
            return $this->error('创建试卷失败');
        }
    }

    /**
     * 编辑试卷页面
     * 
     * @return 
     * @date Sun Jun 10 21:03:16 2018
     */
    public function editpaper(){
        $id = $this->req->param('id');
        
        $paperModel = new \app\admin\model\Paper;
        $result = $paperModel->field(['pap_id','pap_name','pap_type','pap_mark','pap_createtime'])->where(['pap_id'=>$id])->find();
        if($result){
            $this->assign('result',$result);
            return $this->fetch();
        }else{
            return $this->error('获取数据失败');
        }
        
    }

    /**
     * 更新试卷的信息。（不是试题）；
     * 
     * @return success/error html
     * @date Sun Jun 10 21:24:23 2018
     */
    public function updatepaper()
    {
        
        $_data = $this->req->post();
        $id = $_data['id'];
        $data['pap_name'] = $_data['name'];
        $data['pap_type'] = $_data['type'];
        $data['pap_mark'] = $_data['mark'];
        $data['pap_createtime'] = time();
        $paperModel = new \app\admin\model\Paper;
        $result = $paperModel->where(['pap_id'=>$id])->update($data);
        if($result){
            return $this->success('试卷更新成功');
        }else{
            return $this->error('试卷更新失败');
        }
        
    }

    /**
     * 显示某个试卷的试题(选择题)；
     * 
     * @return html
     * @date Sun Jun 10 21:29:47 2018
     */
    public function listchoice()
    {
        $_data = $this->req->get();
        $id = $_data['id'];
        $paperModel = new \app\admin\model\Choice;
        $data = $paperModel->where(['pap_id'=>$id])->select();
        if($data){
            $this->assign('data',$data);
            return $this->fetch();
        }else{
            return $this->error('没有数据');
        }

        
    }
    
    /**
     * 填写选择题的静态页面
     * 
     * @return 
     * @date Sat Jun  9 19:56:12 2018
     */
    public function choice(){
        $id = $this->req->get('id');
        $this->assign('pid',$id);
        return $this->fetch();
    }

    /**
     * 插入新增试题，接受方式：post
     * 
     * @return 
     * @date Sat Jun  9 20:01:09 2018
     */
    public function insertquestion()
    {
        $_data                 = $this->req->post();
        $data['ch_title']      = $_data['title'];
        $data['ch_options']    = serialize($_data['options']);
        $data['ch_result']     = isset($_data['result']) ? serialize($_data['result']) : 0;
        $data['ch_createtime'] = time();
        $data['pap_id']        = $_data['pid'];
        // $data['ch_value'] = serialize($temp);
        $choiceModel = new \app\admin\model\Choice;
        $result = $choiceModel->save($data);
        $result = 1;
        if($result){
            return json(['error'=>0]);
        }else{
            return json(['error'=>1]);
        }
    }

    public function updatequestion(){
        $_data                 = $this->req->post();
        $chid                  = $_data['chid'];
        $data['ch_title']      = $_data['title'];
        $data['ch_options']    = serialize($_data['options']);
        $data['ch_result']     = isset($_data['result']) ? serialize($_data['result']) : 0;
        $data['ch_createtime'] = time();
        $data['pap_id']        = $_data['pid'];

        $choiceModel = new \app\admin\model\Choice;
        $result = $choiceModel->where(['ch_id'=>$chid])->update($data);
        if($result){
            echo $this->alert(1,'更新成功');
        }else{
            echo $this->alert(0,'更新失败');
        }
        
    }

    public function ea(){
        $choiceModel = new \app\admin\model\Choice;
        $data = $choiceModel->where(['ch_id'=>7])->find();
        var_dump(unserialize($data['ch_value']));
        var_dump(unserialize($data['ch_result']));

    }

    public function nl(){
        $a = 0;
        $b = '0';
        if($a==$b){
            echo "right";
        }else{
            echo "wrong";
        }
    }

    /**
     * 添加选择题ajax模板方法;
     * 
     * @return html/template
     * @date Sat Jun  9 20:10:15 2018
     */
    public function _choice()
    {
        echo $this->fetch('_choice');
    }

    /**
     * 编辑题目；
     * 
     * @return 
     * @date Wed Jun 20 16:17:20 2018
     */
    public function editchoice(){
        $id = $this->req->param('id');
        $model = new \app\admin\model\Choice;
        $data = $model->getchoice($id);
        // var_dump($data['ch_result']);
        // var_dump($data['ch_options']);
        // var_dump($data);
        foreach($data['ch_options'] as $key=>$value){
            $temp[$key] = [
                    'option' => $value,
                    'check'=>'',
            ];
        }
        foreach($temp as $key=>$value){
            if(isset($data['ch_result'][$key])){
                $k = ($data['ch_result'][$key]);

                $temp[$k]['check'] = 'checked';
            } 
        }
        $data['value'] = $temp;
        // var_dump($temp);
        $this->assign('data',$data);
        return $this->fetch();
    }

    protected function editessay($id,$type){
        
    }
    public function _name(){
        
    }

    public function insert2(){
        var_dump($_POST);
    }

}

